﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using PSTS.Models;

namespace PSTS.UI.ViewModels
{
    public class TestChapterSelectionPageViewModel: DependencyObject
    {
        public TestChapterSelectionPageViewModel()
        {
            ChaptersList = Repository.Instance.GetAllChaptersList();

            SelectedChapters = new ObservableCollection<Chapter>();
            SelectedChapters.CollectionChanged += SelectedChapters_CollectionChanged;
            SelectedTopics = new ObservableCollection<Topic>();
        }



        #region properties

        public List<Chapter> ChaptersList
        {
            get { return (List<Chapter>) GetValue(ChaptersListProperty); }
            set { SetValue(ChaptersListProperty, value); }
        }
        public static readonly DependencyProperty ChaptersListProperty = DependencyProperty.Register(
                                                                        "ChaptersList",
                                                                        typeof(List<Chapter>),
                                                                        typeof(TestChapterSelectionPageViewModel),
                                                                        new PropertyMetadata(null));

        public List<Topic> TopicsList
        {
            get { return (List<Topic>)GetValue(TopicsListProperty); }
            set { SetValue(TopicsListProperty, value); }
        }
        public static readonly DependencyProperty TopicsListProperty = DependencyProperty.Register(
                                                                "TopicsList",
                                                                typeof(List<Topic>),
                                                                typeof(TestChapterSelectionPageViewModel),
                                                                new PropertyMetadata(null));


        public ObservableCollection<Chapter> SelectedChapters { get; set; }

        public ObservableCollection<Topic> SelectedTopics { get; set; }

        public string ChapterName
        {
            get { return (string)GetValue(ChapterNameProperty); }
            set { SetValue(ChapterNameProperty, value); }
        }
        public static readonly DependencyProperty ChapterNameProperty = DependencyProperty.Register(
                                                                        "ChapterName",
                                                                        typeof(string),
                                                                        typeof(TestChapterSelectionPageViewModel),
                                                                        new PropertyMetadata("",ChapterName_ChangedCallback));
        public string TopicName
        {
            get { return (string)GetValue(TopicNameProperty); }
            set { SetValue(TopicNameProperty, value); }
        }
        public static readonly DependencyProperty TopicNameProperty = DependencyProperty.Register(
                                                                        "TopicName",
                                                                        typeof(string),
                                                                        typeof(TestChapterSelectionPageViewModel),
                                                                        new PropertyMetadata("", TopicName_ChangedCallback));

        public int QuestionsNumber
        {
            get { return (int)GetValue(QuestionsNumberProperty); }
            set { SetValue(QuestionsNumberProperty, value); }
        }
        public static readonly DependencyProperty QuestionsNumberProperty = DependencyProperty.Register(
                                                                        "QuestionsNumber",
                                                                        typeof(int),
                                                                        typeof(TestChapterSelectionPageViewModel),
                                                                        new PropertyMetadata(0));

        public bool WithTimer
        {
            get { return (bool)GetValue(WithTimerProperty); }
            set { SetValue(WithTimerProperty, value); }
        }
        public static readonly DependencyProperty WithTimerProperty = DependencyProperty.Register(
                                                                        "WithTimer",
                                                                        typeof(bool),
                                                                        typeof(TestChapterSelectionPageViewModel),
                                                                        new PropertyMetadata(false));
        public int MM
        {
            get { return (int)GetValue(MMProperty); }
            set { SetValue(MMProperty, value); }
        }
        public static readonly DependencyProperty MMProperty = DependencyProperty.Register(
                                                                        "MM",
                                                                        typeof(int),
                                                                        typeof(TestChapterSelectionPageViewModel),
                                                                        new PropertyMetadata(0));
        public int SS
        {
            get { return (int)GetValue(SSProperty); }
            set { SetValue(SSProperty, value); }
        }
        public static readonly DependencyProperty SSProperty = DependencyProperty.Register(
                                                                        "SS",
                                                                        typeof(int),
                                                                        typeof(TestChapterSelectionPageViewModel),
                                                                        new PropertyMetadata(0));

        public bool SelectAll
        {
            get { return (bool)GetValue(SelectAllProperty); }
            set { SetValue(SelectAllProperty, value); }
        }
        public static readonly DependencyProperty SelectAllProperty = DependencyProperty.Register(
                                                                        "SelectAll",
                                                                        typeof(bool),
                                                                        typeof(TestChapterSelectionPageViewModel),
                                                                        new PropertyMetadata(false));



        #endregion

        #region methods


        #endregion

        #region handlers
        private static void ChapterName_ChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var viewModel = sender as TestChapterSelectionPageViewModel;
            if (viewModel != null)
            {
                viewModel.ChaptersList = Repository.Instance.FindChaptersList(viewModel.ChapterName);
            }

        }

        private static void TopicName_ChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var viewModel = sender as TestChapterSelectionPageViewModel;
            if (viewModel != null)
            {
                viewModel.TopicsList = Repository.Instance.FindTopicsListForChapters(viewModel.SelectedChapters,
                    viewModel.TopicName);
            }
        }

        void SelectedChapters_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            TopicsList = Repository.Instance.GetAllTopicsListForChapters(SelectedChapters);
            TopicName = "";
        }



        #endregion

        public List<Question> GetQuestions()
        {
            try
            {
                var res = Repository.Instance.GetRandomListOfTopicsQuestions(SelectedTopics, QuestionsNumber);
                return res;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            return null;
        }
    }
}
